// Copyright 2012 Google Inc. All Rights Reserved.

package com.google.appengine.syncserver.entities;

import com.google.appengine.api.datastore.Entity;

/**
 * Metadata class the user.
 * Currently contains the max client id issued till date.
 */
public class UserMetadata {
  // max client id property key.
  private static final String MAX_CLIENT_ID = "clientid";

  // Underlying UserMetadata Entity.
  private final Entity entity;

  /**
   * @param entity Underlying datastore entity which defines the metadata.
   */
  public UserMetadata(Entity entity) {
    this.entity = entity;
    if (!entity.hasProperty(MAX_CLIENT_ID)) {
      entity.setProperty(MAX_CLIENT_ID, new Long(0));
    }
  }

  /**
   * @return The entity which defines the metadata.    
   */
  public Entity getEntity() {
    return entity;
  }

  /**
   * Increment the client id in the entity.
   */
  public long incrementMaxClientId() {
    long nextClientId = getMaxClientId() + 1;
    entity.setProperty(MAX_CLIENT_ID, nextClientId);
    return nextClientId;
  }

  /**
   * Get the current max client id.
   * 
   * @return The max client id.
   */
  private long getMaxClientId() {
    return (Long) entity.getProperty(MAX_CLIENT_ID);
  }
}
